<template>
  <div class="ChildPage TablePage" v-loading="loading">

    <vb-page ref="page">

      <template #headerSearch-prepend="scope">
        <el-button size="small" v-loading="supplierLoading" @click="reloadSupplier? getSupplierReloadResult(true): doSupplierSync()" :icon="reloadSupplier? 'el-icon-loading': 'el-icon-refresh'">{{ reloadSupplier? '同步中，点击刷新': '同步供应商' }}</el-button>
<!--        <el-button size="small" type="primary" @click="gotoGain()" :icon="'el-icon-map-location'">获取海域坐标</el-button>-->
<!--        <el-button size="small" type="primary" @click="gotoMap()" :icon="'el-icon-map-location'">测试地图</el-button>-->
        <div style="flex-grow: 1; width: 100%;"></div>
      </template>
      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="open(scope.row.id)">选择坐标</el-button>
        <el-button v-if="scope.row.isDelete === 'false'" size="mini" type="text" style="color:var(--el-color-success)" icon="el-icon-share" @click="deleteSupplier(scope.row.fNumber)">删除</el-button>
        <el-button v-if="scope.row.isDelete === 'true'" size="mini" type="text" style="color: #66CCFF" icon="el-icon-share" @click="restore(scope.row.fNumber)">恢复</el-button>
      </template>
      <template #table-isDelete="scope">
        <el-tag :type="scope.row.isDelete === 'true'? 'success': 'danger'">{{ scope.row.isDelete  === 'true'?  "是": "否" }}</el-tag>
      </template>
    </vb-page>
  </div>
</template>

<script >
import MapContainer from "../components/MapContainer.vue";
export default {
  components: {MapContainer},
  data() {
    return {
      supplierLoading: false,
      reloadSupplier: false,
    }
  },
  methods: {
    async restore (fNumber){
      this.loading = true
      try {
        let res = await this.callApi('restore_supplier', {fNumber : fNumber})
        if (!res.res){
          return
        }
        this.$message.success(`恢复成功请刷新查看`)

      } finally {
        this.loading = false
      }
    },
    async deleteSupplier(fNumber){
      this.loading = true
      try {
        let res = await this.callApi('delete_supplier', {fNumber : fNumber})
        if (!res.res){
          return
        }
        this.$message.warning(`删除成功请刷新查看`)

      } finally {
        this.loading = false
      }
    },
    open(id) {
      window.open(import.meta.env.VITE_EXPORT_URL + `baiduMapTest?id=${ id}`)//
  },
    gotoDetail(fName = '') {
      let url = '/basic/supplierDetail'
      if (fName) {
        url += `?fName=${ fName }`
      }
      this.$router.push(url)
    },
    /**
     * 获取加载按钮的加载中状态
     */
    async getSupplierReloadResult(showMsg) {
      this.supplierLoading = true
      try {
        let res = await this.callApi("get_supplier_reload_result")
        if (!res.res) return
        this.reloadSupplier = res.reloadSupplier
        this.reloadSupplierRes = res.reloadSupplierRes
        this.reloadSupplierMsg = res.reloadSupplierMsg
        this.reloadSupplierStartTime = res.reloadSupplierStartTime
        this.reloadSupplierEndTime = res.reloadSupplierEndTime
        if (showMsg) {
          if (this.reloadSupplier) {
            this.$message.info('供应商数据同步中')
          } else {
            if (this.reloadSupplierRes) {
              this.reload()
              this.$message.success('已同步完成, 请刷新最新结果')
            } else {
              this.$message.error('同步失败，' + this.reloadSupplierMsg)
            }
          }
        }
      } finally {
        this.supplierLoading = false
      }
    },
    /**
     * 同步供应商
     * */
    async doSupplierSync() {
      try {
        let msg = '';
        if (this.reloadSupplierStartTime) {
          msg += `<span style="font-weight: bold;">同步供应商，耗时较长，是否继续执行？</span><br />
          前次同步${ this.reloadSupplierRes? '成功!': `<span style="color: var(--danger)">失败!</span>` } ${ this.reloadSupplierEndTime }
          ${ this.reloadSupplierRes? '': `<br />${ this.reloadSupplierMsg }` }
          <br />`;
        } else {
          msg = '同步供应商，耗时较长，是否继续执行？';
        }
        await this.$confirm(msg, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          dangerouslyUseHTMLString: true
        })
      } catch(e) {
        return
      }
      try {
        this.loading = true
        let res = await this.callApi("sync_supplier")
        if (!res.res) return
        this.reloadSupplier = true
        this.$message.success('已经开始同步')
      } finally {
        this.loading = false
      }
    },
  }
}
</script>

<style scoped>

</style>
